//
//  SLStatusRetweetView.swift
//  langligelang
//
//  Created by 秦－政 on 2016/10/27.
//  Copyright © 2016年 pete. All rights reserved.
//

import UIKit

class SLStatusRetweetView: UIView {
    
    // 记录约束
    var retweetViewCons: Constraint?
    
    // 定义属性 共外界赋值
    var statusViewModel:SLStatusViewModel?{
        didSet{
            // 转发正文
//            contentLabel.text = statusViewModel?.retweetContentText
            contentLabel.attributedText = statusViewModel?.retweetAttributedStr
            // 配图
            // 卸载约束
            retweetViewCons?.uninstall()
            if (statusViewModel?.statusModel?.retweeted_status?.pic_urls?.count)! > 0 {
                pictureView.picUrls = statusViewModel?.statusModel?.retweeted_status?.pic_urls
                pictureView.isHidden = false
                self.snp_makeConstraints(closure: { (make) in
                    self.retweetViewCons = make.bottom.equalTo(pictureView).offset(10).constraint
                })
            }else{
                // 没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints(closure: { (make) in
                    self.retweetViewCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
            }
        }
    }
    override init(frame:CGRect) {
        super.init(frame:frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 布局界面
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(10)
        }
        pictureView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }
        self.snp_makeConstraints { (make) in
            self.retweetViewCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        // 获取点击的高亮字符串
        contentLabel.highlightTapAction = { (view, attributedString, range, rect) -> Void in
            //  根据高亮的范围获取对应的字符串
            let text = (attributedString.string as NSString).substring(with: range)
            print(text)
        }
    }
    
    // MARK: - 懒加载控件
    // 转发正文
//    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: SLNormalFontSize, text: "我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看我就是来看看", maxWidth: SLSCREENW - 20)
    private lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        // 指定最大换行宽度
        label.preferredMaxLayoutWidth = SLSCREENW - 20
        // 换行
        label.numberOfLines = 0
        return label
    }()// 使用YYText
    // 配图
    private lazy var pictureView: SLStatusPictureView = {
        let view = SLStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
